//
//  AECommunityDetailViewModel.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/20.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AECommunityDetailViewModel.h"
#import "AE_Solar-Swift.h"

@implementation AECommunityDetailViewModel

- (void)initialize {
    [super initialize];
    
    self.title = [AEStringExten localizedValueWithKey:@"详情"];
}

- (void)setupData {
//    _detailModel = [AECommunityDetailModel new];
//    _detailModel.title = self.newsInfoModel.newsContent;
//    _detailModel.publishTime = @"2024-10-12 12:32:23";
//    _detailModel.images = @[];
//    self.detailModel.commentNum = 3453;
//    self.detailModel.praiseNum = 54464;
    
    self.newsInfoModel.headerHeight = 7;
    
    // 标题
    self.newsInfoModel.newsTitleFont = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
    self.newsInfoModel.newsTitleHeight = [self.newsInfoModel.newsTitle heightForFont:self.newsInfoModel.newsTitleFont width:kScreenW - 15 * 2] + 2;
    self.newsInfoModel.headerHeight = self.newsInfoModel.headerHeight + self.newsInfoModel.newsTitleHeight;
    
//    self.newsInfoModel.newsContentFont = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
//    self.newsInfoModel.newsContentHeight = [self.newsInfoModel.newsContent heightForFont:self.newsInfoModel.newsContentFont width:kScreenW - 15 * 2] + 2;
//    self.newsInfoModel.headerHeight = self.newsInfoModel.headerHeight + self.newsInfoModel.newsContentHeight;
    
    self.newsInfoModel.headerHeight = self.newsInfoModel.headerHeight + 3;
    self.newsInfoModel.newsDataTimeHeight = 16;
    self.newsInfoModel.newsDataTimeFont = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    self.newsInfoModel.headerHeight = self.newsInfoModel.headerHeight + self.newsInfoModel.newsDataTimeHeight;
    self.newsInfoModel.headerHeight = self.newsInfoModel.headerHeight + 12;
    self.newsInfoModel.imageWidth = self.imageWH;
    self.newsInfoModel.imageHeight = self.imageWH;
    self.newsInfoModel.imageSpace = 3;
    if (self.newsInfoModel.ossUrls.count > 0) {
        self.newsInfoModel.headerHeight = self.newsInfoModel.headerHeight + [self imagesHeight];
    }
    self.newsInfoModel.headerHeight = self.newsInfoModel.headerHeight + 12;
    self.newsInfoModel.headerHeight = self.newsInfoModel.headerHeight + 22; // 评论
    self.newsInfoModel.headerHeight = self.newsInfoModel.headerHeight + 7;
    
    /*
    _detailModel.comments = [NSMutableArray new];
    AECommunityCommentModel *commentModel = [AECommunityCommentModel new];
    commentModel.cellHeight = 8;
    commentModel.headImgUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F1020%2Fc00e221bj00slmrum0027d000hs00bvg.jpg&thumbnail=660x2147483647&quality=80&type=jpg";
    commentModel.nickName = @"暴龙战士";
    commentModel.cellHeight += 18;
    commentModel.cellHeight += 2;
    commentModel.praiseNum = 4343;
    commentModel.content = @"报道还援引朝鲜国防省发言人的话说，朝方将继续把封闭的南部边境永久要塞化。朝鲜国防省发言人18日称，朝鲜人民军总参谋部决定，边境线附近的炮兵联合部队和担负重要火力任务的部队继续保持完全战斗待命状态。这一决定已得到批准。";
    commentModel.contentWidth = kScreenW - 15 * 2 - 35 - 10;
    commentModel.contentFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    commentModel.contentHeight = [commentModel.content heightForFont:commentModel.contentFont width:commentModel.contentWidth] + 2;
    commentModel.cellHeight += (commentModel.contentHeight < 20 ? 20 : commentModel.contentHeight);
    commentModel.cellHeight += 12;
    [_detailModel.comments addObject:commentModel];
    
    commentModel = [AECommunityCommentModel new];
    commentModel.cellHeight = 8;
    commentModel.headImgUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F1020%2Fc00e221bj00slmrum0027d000hs00bvg.jpg&thumbnail=660x2147483647&quality=80&type=jpg";
    commentModel.nickName = @"暴龙战士";
    commentModel.cellHeight += 18;
    commentModel.cellHeight += 2;
    commentModel.praiseNum = 4343;
    commentModel.content = @"报道还援引朝鲜国防省发言人的话说。";
    commentModel.contentWidth = kScreenW - 15 * 2 - 35 - 10;
    commentModel.contentFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    commentModel.contentHeight = [commentModel.content heightForFont:commentModel.contentFont width:commentModel.contentWidth] + 2;
    commentModel.cellHeight += (commentModel.contentHeight < 20 ? 20 : commentModel.contentHeight);
    commentModel.cellHeight += 12;
    [_detailModel.comments addObject:commentModel];
    
    commentModel = [AECommunityCommentModel new];
    commentModel.cellHeight = 8;
    commentModel.headImgUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F1020%2Fc00e221bj00slmrum0027d000hs00bvg.jpg&thumbnail=660x2147483647&quality=80&type=jpg";
    commentModel.nickName = @"暴龙战士";
    commentModel.cellHeight += 18;
    commentModel.cellHeight += 2;
    commentModel.praiseNum = 4343;
    commentModel.content = @"报道还援引朝鲜国防省发言人的话说，朝方将继续把封闭的南部边境永久要塞化。";
    commentModel.contentWidth = kScreenW - 15 * 2 - 35 - 10;
    commentModel.contentFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    commentModel.contentHeight = [commentModel.content heightForFont:commentModel.contentFont width:commentModel.contentWidth] + 2;
    commentModel.cellHeight += (commentModel.contentHeight < 20 ? 20 : commentModel.contentHeight);
    commentModel.cellHeight += 12;
    [_detailModel.comments addObject:commentModel];
    
    commentModel = [AECommunityCommentModel new];
    commentModel.cellHeight = 8;
    commentModel.headImgUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F1020%2Fc00e221bj00slmrum0027d000hs00bvg.jpg&thumbnail=660x2147483647&quality=80&type=jpg";
    commentModel.nickName = @"暴龙战士";
    commentModel.cellHeight += 18;
    commentModel.cellHeight += 2;
    commentModel.praiseNum = 4343;
    commentModel.content = @"报道还援引朝鲜国防省发言人的话说，朝方将继续把封闭的南部边境永久要塞化。朝鲜国防省发言人18日称，朝鲜人民军总参谋部决定";
    commentModel.contentWidth = kScreenW - 15 * 2 - 35 - 10;
    commentModel.contentFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    commentModel.contentHeight = [commentModel.content heightForFont:commentModel.contentFont width:commentModel.contentWidth] + 2;
    commentModel.cellHeight += (commentModel.contentHeight < 20 ? 20 : commentModel.contentHeight);
    commentModel.cellHeight += 12;
    [_detailModel.comments addObject:commentModel];
    
    commentModel = [AECommunityCommentModel new];
    commentModel.cellHeight = 8;
    commentModel.headImgUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F1020%2Fc00e221bj00slmrum0027d000hs00bvg.jpg&thumbnail=660x2147483647&quality=80&type=jpg";
    commentModel.nickName = @"暴龙战士";
    commentModel.cellHeight += 18;
    commentModel.cellHeight += 2;
    commentModel.praiseNum = 4343;
    commentModel.content = @"报道还援引朝鲜国防省发言人的话说，朝方将继续把封闭的南部边境永久要塞化。朝鲜国防省发言人18日称，朝鲜人民军总参谋部决定";
    commentModel.contentWidth = kScreenW - 15 * 2 - 35 - 10;
    commentModel.contentFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    commentModel.contentHeight = [commentModel.content heightForFont:commentModel.contentFont width:commentModel.contentWidth] + 2;
    commentModel.cellHeight += (commentModel.contentHeight < 20 ? 20 : commentModel.contentHeight);
    commentModel.cellHeight += 12;
    [_detailModel.comments addObject:commentModel];
    
    commentModel = [AECommunityCommentModel new];
    commentModel.cellHeight = 8;
    commentModel.headImgUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F1020%2Fc00e221bj00slmrum0027d000hs00bvg.jpg&thumbnail=660x2147483647&quality=80&type=jpg";
    commentModel.nickName = @"暴龙战士";
    commentModel.cellHeight += 18;
    commentModel.cellHeight += 2;
    commentModel.praiseNum = 4343;
    commentModel.content = @"报道还援引朝鲜国防省发言人的话说，朝方将继续把封闭的南部边境永久要塞化。朝鲜国防省发言人18日称，朝鲜人民军总参谋部决定";
    commentModel.contentWidth = kScreenW - 15 * 2 - 35 - 10;
    commentModel.contentFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    commentModel.contentHeight = [commentModel.content heightForFont:commentModel.contentFont width:commentModel.contentWidth] + 2;
    commentModel.cellHeight += (commentModel.contentHeight < 20 ? 20 : commentModel.contentHeight);
    commentModel.cellHeight += 12;
    [_detailModel.comments addObject:commentModel];
    
    commentModel = [AECommunityCommentModel new];
    commentModel.cellHeight = 8;
    commentModel.headImgUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F1020%2Fc00e221bj00slmrum0027d000hs00bvg.jpg&thumbnail=660x2147483647&quality=80&type=jpg";
    commentModel.nickName = @"暴龙战士";
    commentModel.cellHeight += 18;
    commentModel.cellHeight += 2;
    commentModel.praiseNum = 4343;
    commentModel.content = @"报道还援引朝鲜国防省发言人的话说，朝方将继续把封闭的南部边境永久要塞化。朝鲜国防省发言人18日称，朝鲜人民军总参谋部决定";
    commentModel.contentWidth = kScreenW - 15 * 2 - 35 - 10;
    commentModel.contentFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    commentModel.contentHeight = [commentModel.content heightForFont:commentModel.contentFont width:commentModel.contentWidth] + 2;
    commentModel.cellHeight += (commentModel.contentHeight < 20 ? 20 : commentModel.contentHeight);
    commentModel.cellHeight += 12;
    [_detailModel.comments addObject:commentModel];
    
    commentModel = [AECommunityCommentModel new];
    commentModel.cellHeight = 8;
    commentModel.headImgUrl = @"https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2024%2F1020%2Fc00e221bj00slmrum0027d000hs00bvg.jpg&thumbnail=660x2147483647&quality=80&type=jpg";
    commentModel.nickName = @"暴龙战士";
    commentModel.cellHeight += 18;
    commentModel.cellHeight += 2;
    commentModel.praiseNum = 4343;
    commentModel.content = @"报道还援引朝鲜国防省发言人的话说，朝方将继续把封闭的南部边境永久要塞化。朝鲜国防省发言人18日称，朝鲜人民军总参谋部决定";
    commentModel.contentWidth = kScreenW - 15 * 2 - 35 - 10;
    commentModel.contentFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    commentModel.contentHeight = [commentModel.content heightForFont:commentModel.contentFont width:commentModel.contentWidth] + 2;
    commentModel.cellHeight += (commentModel.contentHeight < 20 ? 20 : commentModel.contentHeight);
    commentModel.cellHeight += 12;
    [_detailModel.comments addObject:commentModel];
     */
    
}

- (float)imageWH
{
    if (_imageWH)
    {
        return _imageWH;
    }
    
    _imageWH = (kScreenWidth - 15 * 2 - 3 * 2) / 3.0;
    return _imageWH;
}

/**
 图片的视图的总高度

 @return return value description
 */
- (CGFloat)imagesHeight
{
    if (self.newsInfoModel == nil) {
        return self.imageWH;
    }
    // 图片
    if (self.newsInfoModel.ossUrls.count <= 3)
    {
        return self.imageWH;
    }
    else if (self.newsInfoModel.ossUrls.count > 3 && self.newsInfoModel.ossUrls.count <= 6)
    {
        return self.imageWH * 2 + 3;
    }
    else
    {
        return self.imageWH * 3 + 3 * 2;
    }
    return self.imageWH;
}

/// 评论列表
/// - Parameter completion: completion description
- (void)requestWithNewsComments:(RequestCompletionBlock)completion {
    @weakify(self);
    AENewsCommentsRequest *request = [[AENewsCommentsRequest alloc] initWithNewId:self.newsInfoModel.id];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            NSArray *list = respond.data;
            [self.newsInfoModel.comments removeAllObjects];
            for (AENewsCommentsModel *model in list) {
                model.cellHeight = 8;
                model.cellHeight += 18;
                model.cellHeight += 2;
                model.commentsWidth = kScreenW - 15 * 2 - 35 - 10;
                model.commentsFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
                model.commentsHeight = [model.comments heightForFont:model.commentsFont width:model.commentsWidth] + 2;
                model.cellHeight += (model.commentsHeight < 20 ? 20 : model.commentsHeight);
                model.cellHeight += 12;
                model.cellHeight += 5;
                model.cellHeight += 16;
                [self.newsInfoModel.comments addObject:model];
            }
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 点赞/取消
/// - Parameters:
///   - newsId: 新闻id
///   - like: 1 赞；1 取消
///   - completion: completion description
- (void)requestWithNewsLikeNewsId:(NSInteger)newsId like:(NSInteger)like completion:(RequestCompletionBlock)completion {
    @weakify(self);
    AENewsLikeRequest *request = [[AENewsLikeRequest alloc] initWithNewsId:newsId like:like];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

- (void)requestWithNewsLikeCommentId:(NSInteger)commentId like:(NSInteger)like completion:(RequestCompletionBlock)completion {
    @weakify(self);
    AENewsCommentsLikeRequest *request = [[AENewsCommentsLikeRequest alloc] initWithCommentId:commentId like:like];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 添加评论
/// - Parameters:
///   - comment: 评论内容
///   - completion: completion description
- (void)requestWithAddComment:(NSString *)comment completion:(RequestCompletionBlock)completion {
    AENewsAddCommentReqModel *reqModel = [AENewsAddCommentReqModel new];
    reqModel.newsId = self.newsInfoModel.id;
    reqModel.comments = comment;
    @weakify(self);
    AENewsAddCommentRequest *request = [[AENewsAddCommentRequest alloc] initWithModel:reqModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            AENewsCommentsModel *model = respond.data;
            model.cellHeight = 8;
            model.cellHeight += 18;
            model.cellHeight += 2;
            model.commentsWidth = kScreenW - 15 * 2 - 35 - 10;
            model.commentsFont = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
            model.commentsHeight = [model.comments heightForFont:model.commentsFont width:model.commentsWidth] + 2;
            model.cellHeight += (model.commentsHeight < 20 ? 20 : model.commentsHeight);
            model.cellHeight += 12;
            model.cellHeight += 5;
            model.cellHeight += 16;
            [self.newsInfoModel.comments insertObject:model atIndex:0];
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 浏览获取积分
/// - Parameter completion: completion description
- (void)requestWithNewsBrowse:(RequestCompletionBlock)completion {
    @weakify(self);
    AENewsBrowseRequest *request = [[AENewsBrowseRequest alloc] init];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
